import Vue from 'vue'
import router from '@/router'
import store from '@/store'

/**
 * 获取uuid
 */
export function getUUID () {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
    return (c === 'x' ? (Math.random() * 16 | 0) : ('r&0x3' | '0x8')).toString(16)
  })
}

/**
 * 获取动态高度
 * @param self window
 * @param doc document
 * @param offset 高度偏移量
 * @param callback 回调返回当前高度
 */
export function getDynamicHeight(self, doc, offset, callback) {
  let mainTableHeight =
    (self.innerHeight ||
      doc.documentElement.clientHeight ||
      doc.body.clientHeight) - offset;
  // if 解决分辨率低的屏幕会出现高度小的问题
  if (mainTableHeight < 330) {
    mainTableHeight = mainTableHeight * 1 + 90
  }
  callback(mainTableHeight);
  self.onresize = () => {
    mainTableHeight =
      (self.innerHeight ||
        doc.documentElement.clientHeight ||
        doc.body.clientHeight) - offset;
    callback(mainTableHeight);
  };
}

/**
 * @param self
 */
export function unbindResize(self) {
  self.onresize = null;
}

/**
 * 获取加载组件
 * @param that
 * @param text
 * @returns {ElLoadingComponent | undefined | *}
 */
export function getLoading(that, text='请求中') {
  return that.$loading({
    lock: true,
    text: text,
    spinner: 'el-icon-loading',
    background: '#fff'
  });
}

/**
 * 是否有权限
 * @param {*} key
 */
export function isAuth (key) {
  return JSON.parse(sessionStorage.getItem('permissions') || '[]').indexOf(key) !== -1 || false
}

/**
 * 树形数据转换
 * @param {*} data
 * @param {*} id
 * @param {*} pid
 */
export function treeDataTranslate (data, id = 'id', pid = 'parentId') {
  var res = []
  var temp = {}
  for (var i = 0; i < data.length; i++) {
    temp[data[i][id]] = data[i]
  }
  for (var k = 0; k < data.length; k++) {
    if (temp[data[k][pid]] && data[k][id] !== data[k][pid]) {
      if (!temp[data[k][pid]]['children']) {
        temp[data[k][pid]]['children'] = []
      }
      if (!temp[data[k][pid]]['_level']) {
        temp[data[k][pid]]['_level'] = 1
      }
      data[k]['_level'] = temp[data[k][pid]]._level + 1
      temp[data[k][pid]]['children'].push(data[k])
    } else {
      res.push(data[k])
    }
  }
  return res
}

/**
 * 清除登录信息
 */
export function clearLoginInfo () {
  Vue.cookie.delete('token')
  store.commit('resetStore')
  router.options.isAddDynamicMenuRoutes = false
}

/**
 * 获取当前页面table高度
 * @param self window
 * @param doc document
 * @param offset 高度偏移量
 * @param callback 回调返回当前高度
 */
export function getHeight(self, doc, offset, callback) {
  let mainTableHeight =
    (self.innerHeight ||
      doc.documentElement.clientHeight ||
      doc.body.clientHeight) - offset;
  callback(mainTableHeight);
  self.onresize = () => {
    mainTableHeight =
      (self.innerHeight ||
        doc.documentElement.clientHeight ||
        doc.body.clientHeight) - offset;
    callback(mainTableHeight);
  };
}
export function exportExcel(url,obj) {
  var isFirst = true
  for (let key in obj){
    if(obj[key]){
      if(isFirst){
        url += ('?' + key + '=' + obj[key])
      }else{
        url += ('&' + key + '=' + obj[key])
      }
      isFirst = false
    }

  }
  window.open(url)
}
